/**
 *  svhttpd         A Secure & Vitual http server
 *
 *  Copyright (c)   2007-2008 Dean To <svhttpd@gmail.com>
 *                  All Rights Reserved
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Filename        : misc.h
 *  Version         :
 *  Created         :
 *  Last Modified   :
 *
 *  HISTORY:
 *
 */

#ifndef _MISC_H_
#define _MISC_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <ctype.h>
#include <sys/time.h>

struct slist_t {
    struct slist_t *next;
    void *data;
};

char hex2asc(int i);
int is_ip_string(const char *ips);
int is_port_string(const char *ports);

/**
 *  algorithm.c
 */
struct node_t {
    struct node_t *lchild, *rchild;
    int height;
};

void *sortsearch(void *base, size_t nmemb, size_t size, const void *data, 
                 int (*compar)(const void *data, const void *));
struct node_t *avlinsert(struct node_t *tree, struct node_t *node, 
                         int (*compar)(const void *, const void *));
struct node_t *avlremove(struct node_t *tree, struct node_t *node, 
                         struct node_t **rmnode, 
                         int (*compar)(const void *, const void *));
struct node_t *avlsearch(struct node_t *tree, struct node_t *node, 
                         int (*compar)(const void *, const void *));
struct node_t *avlgetmin(struct node_t *tree);
struct node_t *avlgetmax(struct node_t *tree);

/**
 *  string.c 
 */
struct string_t {
    char *buf;
#define LENGTH_DEF  1024
    int length;
    int index;
    int rest;
#define ADDSTEP_DEF 1024
    int addstep;
};

char *strcpy_overlap(char *dest, const char *src);
char *strtrim(char *src);

struct string_t *string_create(size_t length, size_t addstep);
int string_add(struct string_t *string, const char *add, size_t add_len);
char *string_get(struct string_t *string);
size_t string_len(struct string_t *string);
void string_trunc(struct string_t *string, size_t len);
void string_clear(struct string_t *string);
void string_free(struct string_t *string);
int string_dquote(struct string_t *string);

/**
 *  io.c
 */
int setnoblock(int fd);
/* timeout in milliseconds. */
/**
 *  return value:
 *      >0, read bytes;
 *      0,  EOF;
 *      -1, error;
 *      -2, timeout;
 */
ssize_t readt(int fd, void *buf, size_t count, int *timeout);
/**
 *  return value:
 *      >=0, writen bytes;
 *      -1, error;
 *      -2, timeout;
 */
ssize_t writet(int fd, const void *buf, size_t count, int *timeout);
/**
 *  return value:
 *      >0, ok, read bytes;
 *      0,  EOF;
 *      -1, error;
 *      -2, timeout;
 */
ssize_t readnt(int fd, void *buf, size_t *count, int timeout, 
               int timeout_total);
/**
 *  return value:
 *      >=0, writen bytes;
 *      -1, error;
 *      -2, timeout;
 */
ssize_t writent(int fd, const void *buf, size_t *count, int timeout, 
                int timeout_total);

#endif  /* _MISC_H_ */
